package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.core.entity.jz.Industry;
import com.sxbbc.common.core.entity.jz.IndustryDetail;
import com.sxbbc.common.core.entity.jz.ProcFirstClassifyMain;
import com.sxbbc.common.core.mapper.jz.IndustryDetailMapper;
import com.sxbbc.common.core.mapper.jz.IndustryMapper;
import com.sxbbc.common.core.mapper.jz.ProcFirstClassifyMainMapper;
import com.sxbbc.common.core.service.IIndustryService;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 行业信息主表 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-05-09
 */
@Service
public class IndustryServiceImpl extends ServiceImpl<IndustryMapper, Industry> implements IIndustryService {

    @Autowired
    private IndustryDetailMapper industryDetailMapper;
    @Autowired
    private ProcFirstClassifyMainMapper firstClassifyMainMapper;

    @Override
    public Page<Industry> queryIndustryAndDetail(BasePage<Industry> page, Industry industry) {
        QueryWrapper<Industry> industryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(industry.getName())) {
            industryWrapper.eq("name", industry.getName());
        }
        if (industry.getType() != null && industry.getType() != -1) {
            industryWrapper.eq("type",industry.getType());
        }
        if (StringUtils.isNotBlank(industry.getFirstIds())) {
            industryWrapper.eq("first_ids",industry.getFirstIds());
        }
        if (page.isPaging()) {
            super.page(page, industryWrapper);
        }else{
            List<Industry> list = super.list(industryWrapper);
            page.setRecords(list);
        }
        String[] ids = {};
        if (!CollectionUtils.isEmpty(page.getRecords())) {
            for (Industry i : page.getRecords()) {
                // 查询子行业
                QueryWrapper<IndustryDetail> detailWrapper = new QueryWrapper<>();
                detailWrapper.eq("ind_id", i.getId());
                List<IndustryDetail> detail = industryDetailMapper.selectList(detailWrapper);
                i.setIndustryDetailList(detail);
                // 设置分类名称
                if (StringUtils.isNotBlank(i.getFirstIds())) {
                    ids = i.getFirstIds().split(",");
                    List<Integer> firstIdList = CalcuUtils.getInstance().getIntegerList(ids);
                    QueryWrapper<ProcFirstClassifyMain> firstWrapper = new QueryWrapper<>();
                    firstWrapper.in("id", firstIdList);
                    List<ProcFirstClassifyMain> firstList = firstClassifyMainMapper.selectList(firstWrapper);
                    StringBuffer name = new StringBuffer();
                    for (ProcFirstClassifyMain f : firstList) {
                        name.append(f.getName()).append(",");
                    }
                    i.setClassifyName(name.toString().substring(0, name.length() - 1));
                }
            }
        }
        return page;
    }

    @Override
    public boolean handleIndustryStatus(Integer id, Integer status) {
        Industry industry = super.getById(id);
        industry.setStatus(status);
        return super.updateById(industry);
    }
}
